package com.mz.ozy.activity.me;

import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.mz.ozy.R;
import com.mz.ozy.activity.home.SimpleShareListener;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.AccountBean;
import com.mz.ozy.bean.MeBean;
import com.mz.ozy.bean.NewCheckJoinMemberBean;
import com.mz.ozy.bean.NewResponeBean;
import com.mz.ozy.bean.UnreadMsgBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.MePresenter;
import com.mz.ozy.mvp.view.MeContract;
import com.mz.ozy.utils.BitmapUtils;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.utils.Utils;
import com.mz.ozy.utils.glideUtils.GlideUtils;
import com.mz.ozy.widget.dialog.ShareDialog;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class InviteCodeActivity extends BaseActivity<MeContract.View, MePresenter> implements MeContract.View {

    public static String picUrl;

    @BindView(R.id.qrcode)
    ImageView mQrcode;
    ClipboardManager cm;
    Bitmap mBitmap;
    @BindView(R.id.tv_share_invite_code)
    TextView mTvInviteCode;
    @BindView(R.id.ll_bus_card_top)
    LinearLayout ll_bus_card_top;

    @BindView(R.id.rootView)
    RelativeLayout rootView;
    @BindView(R.id.ll_root)
    LinearLayout llRoot;
    private String shareUrl;
    private String invitationCode;
    private AccountBean dataItem;
    private MeBean mMeBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invite_code);
        ButterKnife.bind(this);
        cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        initTitle2();
        setTitleBarMargin();

        tvBack.setImageResource(R.mipmap.white_back);
        tvCenterTitle.setText("分享码");
        tvCenterTitle.setTypeface(Typeface.DEFAULT_BOLD);
        tvCenterTitle.setTextColor(ContextCompat.getColor(this, R.color.white));

        llRoot.setBackgroundColor(0);
        tvBack.setImageResource(R.mipmap.white_back_icon);
        shareUrl = getIntent().getStringExtra("userlink");
        invitationCode = getIntent().getStringExtra("code");
        mTvInviteCode.setText(invitationCode);
        mQrcode.setImageBitmap(CodeUtils.createImage(Constants.KEY.SHARL + invitationCode, Utils.dp2px(this, 105), Utils.dp2px(this, 105), null));
//        String qrCode = "https://testapi.ouzhuoya.com/abc/qrcode.png";
//        GlideUtils.loadRoundImg(this, mQrcode, qrCode, R.mipmap.ic_launcher, 4);

//        rootView.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                mBitmap = BitmapUtils.createBitmap1(rootView);
//                if (mBitmap != null) {
//                    Uri uri = BitmapUtils.saveImageToGallery(InviteCodeActivity.this, mBitmap);
//                    if (uri != null) {
//                        ToastUtils.showToast(getString(R.string.str_save_photo_success));
//
//                    } else {
//                        ToastUtils.showToast(getString(R.string.str_save_photo_failure));
//
//                    }
//                }
//
//                return false;
//            }
//        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.getMeInfo(this, false);
        mPresenter.upAccountHeader(this);
    }

    @OnClick({R.id.btn_save_share})
    public void onViewClicked(View view) {
        switch (view.getId()) {
//            case R.id.bt_copy_link:
//                cm.setText(Constants.KEY.SHARL + invitationCode);
//                ToastUtils.showToast(getString(R.string.str_copy_success));
//                break;
//            case R.id.bt_share:
//                showShareDialog();
//                break;

            case R.id.btn_save_share:
                mBitmap = BitmapUtils.createBitmap1(ll_bus_card_top);
                if (mBitmap != null) {
                    Uri uri = BitmapUtils.saveImageToGallery(InviteCodeActivity.this, mBitmap);
                    if (uri != null) {
                        ToastUtils.showToast(getString(R.string.str_save_photo_success));
                    } else {
                        ToastUtils.showToast(getString(R.string.str_save_photo_failure));
                    }
                }
                break;
        }
    }

    private ShareDialog mDialog;

    /**
     * 显示分享弹窗
     */
    private void showShareDialog() {
        if (mDialog == null) {
            mDialog = new ShareDialog(this);
            mDialog.setOnDialogListener(new ShareDialog.onDialogListener() {
                @Override
                public void onConfirm(SHARE_MEDIA platform) {
                    shareSingleImage(platform);
                    mDialog.toggleDialog();
                }
            });
        }
        mDialog.toggleDialog();
    }

    public void shareSingleImage(SHARE_MEDIA platform) {
        ShareAction shareAction = new ShareAction(this);
        shareAction.withMedia(getShareBitmap()).setPlatform(platform).setCallback(new SimpleShareListener() {

            @Override
            public void onResult(SHARE_MEDIA shareMedia) {
//                if (shareMedia == SHARE_MEDIA.SINA) {
//                ToastUtils.showToast("分享成功");
//                }
            }

            @Override
            public void onStart(SHARE_MEDIA shareMedia) {
                super.onStart(shareMedia);
                if ((shareMedia == SHARE_MEDIA.WEIXIN || shareMedia == SHARE_MEDIA.WEIXIN_CIRCLE) && !UMShareAPI.get(InviteCodeActivity.this)
                        .isInstall(InviteCodeActivity.this, SHARE_MEDIA.WEIXIN)) {
                    ToastUtils.showToast("您未安装微信客户端");
                }
                if (shareMedia == SHARE_MEDIA.QQ && !UMShareAPI.get(InviteCodeActivity.this).isInstall(InviteCodeActivity.this, SHARE_MEDIA.QQ)) {
                    ToastUtils.showToast("您未安装QQ客户端");
                }
                if (shareMedia == SHARE_MEDIA.SINA && !UMShareAPI.get(InviteCodeActivity.this).isInstall(InviteCodeActivity.this, SHARE_MEDIA.SINA)) {
                    ToastUtils.showToast("您未安装微博客户端");
                }
            }

        }).share();
    }

    private UMWeb getShareBitmap() {
        String yourName = "";
        if (!TextUtils.isEmpty(dataItem.getUname())) {
            yourName = dataItem.getUname();
        } else if (!TextUtils.isEmpty(dataItem.getUsername())) {
            yourName = dataItem.getUsername();
        }
        String userlink = Constants.KEY.SHARL + mMeBean.getInvitecode();
        UMWeb web = new UMWeb(userlink);//连接地址
        web.setTitle(yourName);//标题
        web.setDescription(yourName + "给您分享了他的名片，快来看看吧！");//描述
        if (TextUtils.isEmpty(dataItem.getUseravatar())) {
            web.setThumb(new UMImage(this, R.mipmap.logo)); //本地缩略图
        } else {
            web.setThumb(new UMImage(this, dataItem.getUseravatar())); //网络缩略图
        }
        return web;
    }

    @Override
    public void getgetMeInfoSuccess(MeBean bean) {
        mMeBean = bean;
    }

    @Override
    public void getAccountSuccess(AccountBean bean) {
        dataItem = bean;
    }

    @Override
    public void uploadHeaderSuccess(JSONObject bean) {

    }

    @Override
    public void getxySuccess(MeBean bean) {

    }

    @Override
    public void bindInfo(String type, JSONObject bean) {

    }

    @Override
    public void getAliSuccess(MeBean bean) {

    }

    @Override
    public void exitLoginSuccess(JSONObject bean) {

    }

    @Override
    public void onCancelBindSuccess() {

    }

    @Override
    public void getUnreadMsgSuccess(UnreadMsgBean bean) {

    }

}
